package cn.px.service.upms.impl;


import cn.px.common.util.ListUtils;
import cn.px.dao.dto.upms.SysUserRoleDTO;
import cn.px.dao.entity.upms.SysUserRole;
import cn.px.dao.mapper.upms.SysUserRoleMapper;
import cn.px.dao.vo.SysUserRoleVO;
import cn.px.service.base.impl.BaseServiceImpl;
import cn.px.service.upms.SysUserRoleService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

/**
 * @Description 用户与角色 service 实现类
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRoleMapper, SysUserRole, SysUserRoleDTO> implements SysUserRoleService {

    @Override
    public void saveUserRoles(String userId, List<String> roleIds) {
        List<SysUserRole> sysRoleUserList = ListUtils.newArrayList(roleIds.size());
        roleIds.forEach(roleId -> sysRoleUserList.add(new SysUserRole(userId, roleId)));
        mapper.insertList(sysRoleUserList);
    }

    @Override
    public void deleteByUserId(String userId) {
        Weekend<SysUserRole> weekend = new Weekend<>(SysUserRole.class);
        WeekendCriteria<SysUserRole, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andEqualTo(SysUserRole::getUserId, userId);
        mapper.deleteByExample(weekend);
    }

    @Override
    public List<SysUserRole> selectByUserId(String userId) {
        Weekend<SysUserRole> weekend = new Weekend<>(SysUserRole.class);
        WeekendCriteria<SysUserRole, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andEqualTo(SysUserRole::getUserId, userId);
        return mapper.selectByExample(weekend);
    }

    @Override
    public List<SysUserRoleVO> selectVOByRoleId(String roleId) {
        SysUserRoleDTO sysUserRoleDTO = new SysUserRoleDTO();
        sysUserRoleDTO.setRoleId(roleId);
        return mapper.selectVOByRoleId(sysUserRoleDTO);
    }

    @Override
    public List<SysUserRole> selectListByRoleAndUserStatus(String roleId, Integer status) {
        return mapper.selectListByRoleAndUserStatus(roleId, status);
    }
}
